package teaching.io.advance;

import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Calendar;
import java.util.GregorianCalendar;

import teaching.io.entity.Department;
import teaching.io.entity.Employee;
import teaching.io.entity.Student;

public class RandomAccessFileExample {

	public static void main(String[] args) {
	

	}

}

/**
 * 院系数据访问类。
 * 
 * @author deng.zhao
 *
 */
class DepartmentDAO {
	/**
	 * 编号大小。
	 */
	public static final int ID_SIZE;
	/**
	 * 名字大小。
	 */
	public static final int NAME_SIZE;
	/**
	 * 记录数量。
	 */
	public static final int RECORD_NUMBER;
	/**
	 * 记录大小。
	 */
	public static final int RECORD_SIZE;

	/**
	 * 常量初始化。
	 */
	static {
		RECORD_NUMBER = 100;
		ID_SIZE = 2;
		NAME_SIZE = 20;
		RECORD_SIZE = ID_SIZE + NAME_SIZE;
	}

	/**
	 * 创建文件。
	 */
	public static void createFile() {
		try (RandomAccessFile out = new RandomAccessFile("d:/department.dat", "rw");) {
			for (int i = 0; i < RECORD_NUMBER; i++) {
				for (int j = 0; j < RECORD_SIZE; j++) {
					out.write(0);
				}
			}
		} catch (final FileNotFoundException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

	public void add(Department department) {
//		int employeeId = employee.getId();
//		try (final RandomAccessFile out = new RandomAccessFile("d:/data/department.dat", "rw");) {
//			if (employeeId >= 1 && employeeId <= RECORD_NUMBER) {
//				out.seek((employeeId - 1) * RECORD_SIZE);
//				writeEmployee(employee, out);
//			}
//		} catch (final FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (final IOException e) {
//			e.printStackTrace();
//		}
//
//		f(department);
	}

	private Department read(DataInputStream dis) throws IOException {
		final String id = dis.readUTF();
		final String name = dis.readUTF();
		return new Department(id, name);
	}

	public Department get(String id) {
//		if (id < 0 || id > RECORD_NUMBER) {
//			throw new IllegalArgumentException("编号错误");
//		}

		try (final RandomAccessFile in = new RandomAccessFile("d:/data/student.dat", "r");) {
			in.seek(RECORD_SIZE);

		} catch (final FileNotFoundException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}

		return null;
	}

//	private void f(Department department) {
//		out.writeInt(department.getId());
//		writeFixedString(employee.getName(), NAME_SIZE, out);
//		out.writeDouble(employee.getSalary());
//		out.writeInt(calender.get(Calendar.YEAR));
//		out.writeByte(calender.get(Calendar.MONTH));
//		out.writeByte(calender.get(Calendar.DATE));
//	}

}